package com.cgbl.cg.adapter

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.recyclerview.widget.RecyclerView
import com.cgbl.cg.R
import com.robot.base.configs.AppConstants
import com.robot.base.model.ProjectBean
import com.robot.base.util.arouter.ARouterUtil
import com.robot.base.utils.pic.ImageLoaderUtils
import com.youth.banner.adapter.BannerAdapter

class ProjectDiscountAdapter(mDatas: List<ProjectBean>) :
    BannerAdapter<ProjectBean, ProjectDiscountAdapter.BannerViewHolder>(mDatas) {
    //创建ViewHolder，可以用viewType这个字段来区分不同的ViewHolder
    override fun onCreateHolder(parent: ViewGroup, viewType: Int): BannerViewHolder {
        //注意布局文件，item布局文件要设置为match_parent，这个是viewpager2强制要求的
        //或者调用BannerUtils.getView(parent,R.layout.banner_image_title_num);
        val view: View = LayoutInflater.from(parent.context)
            .inflate(R.layout.app_item_discount_project, parent, false)
        return BannerViewHolder(view)
    }

    //绑定数据
    @SuppressLint("SetTextI18n")
    override fun onBindView(holder: BannerViewHolder, data: ProjectBean, position: Int, size: Int) {
        holder.clLayout.setOnClickListener {
            ARouterUtil.goToActivityWithBundle(AppConstants.Router.HOME.A_CASE_DETAILS,
                Bundle().apply {
                    putString("id", data.id)
                })
        }

        holder.tvName.text = data.title
        holder.tvInfoComment.text = data.infoComment
        holder.tvPrice.text = "￥${data.budget ?: "0.00"}"

        val labelAdapter = LabelAdapter()
        holder.mRv.apply {
            adapter = labelAdapter
        }

        labelAdapter.setNewInstance(data.itemLabel?.split(",") as MutableList<String>?)

        ImageLoaderUtils.displayRound(
            holder.imageView.context,
            holder.imageView,
            if (data.itemCaseFileDOS?.isNotEmpty() == true) {
                data.itemCaseFileDOS?.get(0)?.filePath ?: ""
            } else {
                ""
            },
            8
        )
        //可以在布局文件中自己实现指示器，亦可以使用banner提供的方法自定义指示器，目前样式较少，后面补充
    }

    inner class BannerViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        var imageView: ImageView
        var tvPrice: TextView
        var tvName: TextView
        var mRv: RecyclerView
        var tvInfoComment: TextView
        var clLayout: ConstraintLayout

        init {
            imageView = view.findViewById(R.id.item_iv_1)
            tvPrice = view.findViewById(R.id.item_tv_price)
            tvName = view.findViewById(R.id.item_tv_name)
            mRv = view.findViewById(R.id.mRv)
            tvInfoComment = view.findViewById(R.id.item_tv_infoComment)
            clLayout = view.findViewById(R.id.item_cl_layout)
        }

    }
}